import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.Ordering;
import com.sun.istack.internal.Nullable;

import static com.google.common.base.Objects.*;

import java.util.*;

import static com.google.common.base.Preconditions.*;

/**
 * Created with IntelliJ IDEA.
 * MyUser: belict
 * Date: 11/28/13
 * Time: 4:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class GuavaTest {
    private Integer helloWorldPhrase = Integer.valueOf((int) (Math.random()*100));


    @Override
    public String toString() {
        return "GuavaTest{" +
                "helloWorldPhrase='" + helloWorldPhrase + '\'' +
                '}';
    }

    public void setHelloWorldPhrase(Integer helloWorldPhrase) {
        this.helloWorldPhrase = helloWorldPhrase;
    }

    public static void main(String[] args) {
        Ordering<GuavaTest> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<GuavaTest, Comparable>() {
            @Override
            public Comparable apply(@Nullable GuavaTest guavaTest) {
                return guavaTest.helloWorldPhrase;
            }
        });

        List list = new ArrayList<GuavaTest>();
        for (int i = 0; i < 5; i++) {
            list.add(new GuavaTest());
        }

        GuavaTest gg = new GuavaTest();
        gg.setHelloWorldPhrase(null);

        list.add(gg);

        Collections.sort(list,ordering);

        System.out.println(list);

    }

    private static void preconditionsDemo(){
        try{
            canIGetMoreChocolate(false);
        }catch (IllegalArgumentException e){
            System.out.println("Awesome! Your son now crying!");
        }
    }

    private static void canIGetMoreChocolate(boolean permission) {
        checkArgument(permission);
        System.out.println("Thanks for chocolate lamer");
    }

    private static void optionalDemo() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("some","wyoming");
        map.put("any", null);
        for (String key : map.keySet()) {
            Optional<String> value = Optional.fromNullable(map.get(key));
            System.out.println("bla bla bla " + value.or("Collection does not contain any value"));
        }
    }
}
